
package com.market.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;

import com.market.bean.Product;
import com.market.constant.Constant;
import com.market.web.form.ProductForm;
import com.market.web.tag.Pages;
import com.market.web.utils.ListUtils;

public class AdminProSearchAction extends AdminProductAction
{
    private int perPage = 40;

    public String getCategory()
    {
        return category;
    }

    public void setCategory(String category)
    {
        this.category = category;
    }

    public String getAction()
    {
        return action;
    }

    public void setAction(String action)
    {
        this.action = action;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getOrderBy()
    {
        return orderBy;
    }

    public void setOrderBy(String orderBy)
    {
        this.orderBy = orderBy;
    }

    public String getType()
    {
        return type;
    }

    public void setType(String type)
    {
        this.type = type;
    }

    public ActionErrors validate(ActionMapping mapping, HttpServletRequest reqeust)
    {
      /*  if (!"bowb".equals(category) && !"arrow".equals(category) && !"ball".equals(category)
                && !"collimator".equals(category) && !"safety".equals(category))
        {
            category = "bowa";
        }*/
        
        
        if(logger.isDebugEnabled())
        {
            logger.debug("category xxxxxxxxxx is"+category);
        }
            
        if (!"rice".equals(category) && !"beer".equals(category))
        {
            category = "rice";
        }
        
        if (!"hot".equals(orderBy) && !"adddate".equals(orderBy) && !"vipprice".equals(orderBy))
        {
            orderBy = "price";
        }
        return null;
    }

    public String getParameter()
    {
        return parameter;
    }

    public void setParameter(String parameter)
    {
        this.parameter = parameter;
    }

    private String action;

    private String type;

    private String orderBy;

    private String name;

    private String parameter;

    private String category;
    
    public String execute() throws Exception
    {
        int curP = (int)getLong("page");
        if (curP == 0)
        {
            curP++;
        }
        /*SearchForm sf = new SearchForm();
        BeanUtils.copyProperties(this, sf);*/
        List l = getProductService().findByForm(getCategory(),getType(),getName(),getParameter(),getOrderBy(), (curP - 1) * perPage, perPage);
        int count = getProductService().getCountByForm(getCategory(),getType(),getName(),getParameter(),getOrderBy());
        //setPages(mapping, request, sf, curP, count, perPage);
        setPages(curP, count, Constant.perPage);
        getRequest().setAttribute("productList", ListUtils.convert(l, Product.class, ProductForm.class));
        return SUCCESS;//mapping.findForward("prolist");
    }
    
    protected void setPages( int curP, int count,
            int perPage)
    {
        Pages pages = new Pages();
        pages.setPerPage(perPage);
        pages.setRowCount(count);
        pages.setCurPage(curP);
        StringBuffer path = new StringBuffer();
        path.append(getRequest().getServletPath()).append(".do?").append("category=").append(getCategory()).append("&type=")
                .append(getType()).append("&name=").append(getName()).append("&parameter=").append(
                        getParameter()).append("&orderBy=").append(getOrderBy());
        pages.setPath(path.toString());
        getRequest().setAttribute("pages", pages);
    }
}
